package io.renren.modules.hospital.service.impl;

import io.renren.common.exception.RRException;
import io.renren.common.validator.Assert;
import io.renren.modules.app.form.LoginForm;
import io.renren.modules.hospital.dao.EmrGhInfoDao;
import io.renren.modules.hospital.entity.EmrYsInfoEntity;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hospital.dao.EmrHzInfoDao;
import io.renren.modules.hospital.entity.EmrHzInfoEntity;
import io.renren.modules.hospital.service.EmrHzInfoService;


@Service("emrHzInfoService")
public class EmrHzInfoServiceImpl extends ServiceImpl<EmrHzInfoDao, EmrHzInfoEntity> implements EmrHzInfoService {

    @Autowired
    EmrHzInfoDao hzDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EmrHzInfoEntity> page = this.page(
                new Query<EmrHzInfoEntity>().getPage(params),
                new QueryWrapper<EmrHzInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public EmrHzInfoEntity queryByPhone(String phone) {
        return baseMapper.selectOne(new QueryWrapper<EmrHzInfoEntity>().eq("EMR_hz_phone", phone));
    }

    @Override
    public EmrHzInfoEntity login(LoginForm form) {
        EmrHzInfoEntity user = queryByPhone(form.getMobile());
        Assert.isNull(user, "手机号或密码错误");

        //密码错误
        if(!user.getEmrHzPwd().equals(DigestUtils.sha256Hex(form.getPassword()))){
            throw new RRException("手机号或密码错误");
        }

        return user;
    }

    @Override
    public EmrHzInfoEntity findHzById(Integer hzId) {
        return hzDao.findHzById(hzId);
    }

    @Override
    public List<EmrHzInfoEntity> getHzListByParam(String key, String paramType, int page, int size) {
        List<EmrHzInfoEntity> res = new ArrayList<>();
        if (paramType.equals("EMR_hz_id"))
            res = hzDao.getHzListByHzId(key , (page - 1) * size, size);
        if (paramType.equals("EMR_hz_name"))
            res = hzDao.getHzListByHzName('%' + key + '%', (page - 1) * size, size);
        if (paramType.equals("EMR_hz_phone"))
            res = hzDao.getHzListByHzPhone('%' + key + '%', (page - 1) * size, size);
        if (paramType.equals("EMR_hz_sex"))
            res = hzDao.getHzListByHzSex('%' + key + '%', (page - 1) * size, size);
        return res;
    }

}
